<?php

/**
 * login actions.
 *
 * @package    jobeet
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  
/*   public function executeLogin(sfWebRequest $request)
  {
    $this->form = new LoginForm();

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('login'));
      if ($this->form->isValid())
      {
        // authenticate user and redirect them
        $this->getUser()->setAuthenticated(true);
        $this->getUser()->addCredential('user');
        $this->redirect('artice/index');
      }
    }
  }
  */
  

   public function executeLogin(sfWebRequest $request)
  {
    $this->form = new LoginForm();

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('login'));
      
      if ($this->form->isValid())
      {
        
        $u = $this->form->getValue('username');
        $p = $this->form->getValue('password');
        
        if($this->checkLogin($u, $p)){
            // authenticate user and redirect them
            $this->getUser()->setAuthenticated(true);
            $this->getUser()->addCredential('user');
            $this->redirect('artice/index');
        }else{
            echo "Username or Password không chính xác!";
        }
      }
    }
  }

  
  public function checkLogin($user,$pass )
    {
        $q = Doctrine_core::getTable('BachkimUser')
                ->createQuery()
                ->where('username = ?', $user)
                ->andWhere('password = ?', $pass);
       $result = $q->count();
        
        if($result > 0){
                return true;
            }else{
                return false;
            }
    }
  
  public function executeLogout()
  {
    $this->getUser()->clearCredentials();
    $this->getUser()->setAuthenticated(false);
    $this->redirect('@homepage');
  }
}
